package com.lvyerose.provinceandcity.provinceandcity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lvyerose.provinceandcity.provinceandcity.R;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lvyeRose
 * objective: 城市适配器
 * mailbox: lvyerose@163.com
 * time: 15/6/29 22:24
 */
public class CityAdapter extends BaseAdapter{
    private Context mContext;
    //与城市关联的字母集
    private String[] letter = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    //城市数据集
    List<String> letterToCity=new ArrayList<String>();
    final static int TYPE_1=1;
    final static int TYPE_2=2;


    public CityAdapter(Context mContext, String[] letter, List<String> letterToCity) {
        this.mContext = mContext;
        this.letter = letter;
        this.letterToCity = letterToCity;
    }

    public CityAdapter(Context mContext, List<String> letterToCity) {
        this.mContext = mContext;
        this.letterToCity = letterToCity;
    }

    public CityAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public CityAdapter(Context mContext, String[] letter) {
        this.mContext = mContext;
        this.letter = letter;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return letterToCity.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return letterToCity.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        for(int i=0;i<letter.length;i++){
            if(letterToCity.get(position).equals(letter[i])){
                return TYPE_1;
            }
        }
        return TYPE_2;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /**
         * 即使convertView缓存了一些布局，但是在重构时，根本不知道怎么样去让convertView返回你所需要的布局，这时你需
         * 要让adapter知道我当前有哪些布局，我重构Item时的布局选取规则，好让convertView能返回你需要的布局。
         * 需要重写以下两个函数
         * @Override
         * public int getItemViewType(int position) {}这个函数获取在getView中创建的视图的类型
         * @Override
         * public int getViewTypeCount() {}返回在getView中创建视图类型的数量
         * 至于这两个方法的详细用处，自己看api即可
         */
        ViewHolderOfLetter vhLetter=null;
        ViewHolderOfCity vhCity=null;
        int type=getItemViewType(position);
        if(convertView==null){
            switch (type) {
                case TYPE_1:
                    convertView= LayoutInflater.from(mContext).inflate(R.layout.letter, null);
                    vhLetter=new ViewHolderOfLetter();
                    vhLetter.tv=(TextView)convertView.findViewById(R.id.letterTextView);
                    convertView.setTag(vhLetter);
                    break;
                case TYPE_2:
                    convertView=LayoutInflater.from(mContext).inflate(R.layout.city, null);
                    vhCity=new ViewHolderOfCity();
                    vhCity.tv=(TextView)convertView.findViewById(R.id.cityTextView);
                    convertView.setTag(vhCity);
                    break;
                default:
                    break;
            }
        }else{
            switch (type) {
                case TYPE_1:
                    vhLetter=(ViewHolderOfLetter)convertView.getTag();
                    break;
                case TYPE_2:
                    vhCity=(ViewHolderOfCity)convertView.getTag();
                    break;
                default:
                    break;
            }
        }
        switch (type) {
            case TYPE_1:
                vhLetter.tv.setText(letterToCity.get(position));
                break;
            case TYPE_2:
                vhCity.tv.setText(letterToCity.get(position));
                break;
            default:
                break;
        }
        return convertView;
    }


//    static class ViewHolder{
//        TextView tv;
//        LinearLayout ll;
//    }
    static class ViewHolderOfLetter{
        TextView tv;
    }
    static class ViewHolderOfCity{
        TextView tv;
    }



}

